﻿namespace GuiaTest
{
    using System;
    using System.Windows.Automation;
    using Guia.Core;
    using Guia.Elements;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfTestApplication;

    /// <summary>
    /// Summary description for DataGridTest
    /// </summary>
    [TestClass]
    public class DataGridTest : AbstractWindowTest
    {
        protected override Type TestWindowType()
        {
            return typeof(DataGridTestWindow);
        }

        protected override string TestWindowName()
        {
            return "DataGridTestWindow";
        }

        [TestMethod]
        public void TestGridCellInformation()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            DataGridCell_WpfToolkit cell = dataGrid[2, 1];
            Assert.AreEqual(1, cell.Column);
            Assert.AreEqual(2, cell.Row);
            Assert.AreEqual(1, cell.ColumnSpan);
            Assert.AreEqual(1, cell.RowSpan);
        }

        [TestMethod]
        public void TestGridRowsCellsAccessInformation()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual(3, dataGrid.Rows[0].Cells.Length);
            Assert.AreEqual(2, dataGrid.Rows[0].Cells[2].Column);
            Assert.AreEqual(0, dataGrid.Rows[0].Cells[2].Row);
            Assert.AreEqual(1, dataGrid.Rows[0].Cells[2].RowSpan);
            Assert.AreEqual(1, dataGrid.Rows[0].Cells[2].ColumnSpan);
        }

        [TestMethod]
        public void TestGridHeaderCount()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual(4, dataGrid.Header.ColumnCount);
            Assert.AreEqual(4, dataGrid.Header.HeaderColumns.Length);
        }

        [TestMethod]
        public void TestGridHeaderDirectAccess()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual("Creator", dataGrid.Header["Creator"].Name);
            Assert.AreEqual("Creator", dataGrid.Header[2].Name);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGridHeaderDirectAccessNullString()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            string temp = dataGrid.Header[null].Name;
        }

        [TestMethod]
        public void TestGridHeaderAccessAll()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual("Game Name", dataGrid.Header.HeaderColumns[1].Name);
            Assert.AreEqual("Creator", dataGrid.Header.HeaderColumns[2].Name);
            Assert.AreEqual("Publisher", dataGrid.Header.HeaderColumns[3].Name);
        }

        [TestMethod]
        public void TestGridCellDimensions()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual(4, dataGrid.RowCount);
            Assert.AreEqual(3, dataGrid.ColumnCount);
        }

        [TestMethod]
        public void TestGridCellAccessRead()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual("World Of Warcraft", dataGrid[0, 0].TextValue);
            Assert.AreEqual("Blizzard", dataGrid[0, 1].TextValue);
            Assert.AreEqual("Blizzard", dataGrid[0, 2].TextValue);
            Assert.AreEqual("Halo", dataGrid[1, 0].TextValue);
            Assert.AreEqual("Bungie", dataGrid[1, 1].TextValue);
            Assert.AreEqual("Microsoft", dataGrid[1, 2].TextValue);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestGridCellOutOfBounds()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual("no cell here", dataGrid[8, 0].TextValue);
        }

        [TestMethod]
        public void TestGridRowsAccess()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual(4, dataGrid.Rows.Length);
        }

        [TestMethod]
        public void TestGridRowSelectionMethods()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual(false, dataGrid.Rows[0].IsSelected);
            // With methods
            dataGrid.Rows[0].Select();
            Assert.AreEqual(true, dataGrid.Rows[0].IsSelected);
            dataGrid.Rows[0].Unselect();
            Assert.AreEqual(false, dataGrid.Rows[0].IsSelected);
        }

        [TestMethod]
        public void TestGridRowSelectionIsSelected()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual(false, dataGrid.Rows[0].IsSelected);
            // With attribute
            dataGrid.Rows[0].IsSelected = true;
            Assert.AreEqual(true, dataGrid.Rows[0].IsSelected);
            dataGrid.Rows[0].IsSelected = false;
            Assert.AreEqual(false, dataGrid.Rows[0].IsSelected);
        }

        [TestMethod]
        public void TestGetNestedElement()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            Assert.IsNotNull(dataGrid);
            // textboxes
            Assert.AreEqual("World Of Warcraft", dataGrid.Rows[0].Cells[0].GetNestedElement<Label>().TextValue);
            Assert.AreEqual("World Of Warcraft", dataGrid[0, 0].GetNestedElement<Label>().TextValue);
            // checkboxes
            Assert.AreEqual(true, dataGrid[0, 3].GetNestedElement<CheckBox>().IsChecked);
            Assert.AreEqual(false, dataGrid[1, 3].GetNestedElement<CheckBox>().IsChecked);
        }

        [TestMethod]
        [ExpectedException(typeof(ElementNotAvailableException))]
        public void TestGetNestedElementNotFound()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            Assert.IsNotNull(dataGrid);
            dataGrid[0, 3].GetNestedElement<Label>();
        }

        [TestMethod]
        public void TestGridRowSelectionSingleSelectMode()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual(false, dataGrid.Rows[0].IsSelected);
            dataGrid.Rows[0].Select();
            Assert.AreEqual(true, dataGrid.Rows[0].IsSelected);
            dataGrid.Rows[0].Unselect();
            Assert.AreEqual(false, dataGrid.Rows[0].IsSelected);
        }

        [TestMethod]
        public void TestGridCellSelection()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual(false, dataGrid[1, 1].IsSelected);
            dataGrid[1, 1].Select();
            Assert.AreEqual(true, dataGrid[1, 1].IsSelected);
            dataGrid[1, 1].Unselect();
            Assert.AreEqual(false, dataGrid[1, 1].IsSelected);
            dataGrid[1, 1].IsSelected = true;
            Assert.AreEqual(true, dataGrid[1, 1].IsSelected);
            dataGrid[1, 1].IsSelected = false;
            Assert.AreEqual(false, dataGrid[1, 1].IsSelected);
        }

        [TestMethod]
        [ExpectedException(typeof(OperationNotSupportedException))]
        public void TestGridCellSelectionNotSupported()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual(false, dataGrid[1, 1].IsSelected);
        }

        [TestMethod]
        public void TestGridMultipleCellSelection()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            Assert.IsNotNull(dataGrid);
            Assert.AreEqual(false, dataGrid[1, 1].IsSelected);
            // Select 1 cell
            dataGrid[1, 1].Select();
            Assert.AreEqual(true, dataGrid[1, 1].IsSelected);
            // Select another cell
            dataGrid[2, 1].Select();
            Assert.AreEqual(false, dataGrid[1, 1].IsSelected);
            Assert.AreEqual(true, dataGrid[2, 1].IsSelected);
            // Add another cell to selection
            dataGrid[1, 1].AddToSelection();
            dataGrid[1, 2].AddToSelection();
            dataGrid[2, 2].AddToSelection();
            Assert.AreEqual(true, dataGrid[1, 1].IsSelected);
            Assert.AreEqual(true, dataGrid[2, 1].IsSelected);
            Assert.AreEqual(true, dataGrid[1, 2].IsSelected);
            Assert.AreEqual(true, dataGrid[2, 2].IsSelected);
            // Remove from selection
            dataGrid[2, 2].IsSelected = false;
            Assert.AreEqual(true, dataGrid[1, 1].IsSelected);
            Assert.AreEqual(true, dataGrid[2, 1].IsSelected);
            Assert.AreEqual(true, dataGrid[1, 2].IsSelected);
            Assert.AreEqual(false, dataGrid[2, 2].IsSelected);
            // Set property
            dataGrid[3, 3].IsSelected = true;
            Assert.AreEqual(false, dataGrid[1, 1].IsSelected);
            Assert.AreEqual(false, dataGrid[2, 1].IsSelected);
            Assert.AreEqual(false, dataGrid[1, 2].IsSelected);
            Assert.AreEqual(false, dataGrid[2, 2].IsSelected);
            Assert.AreEqual(true, dataGrid[3, 3].IsSelected);
        }

        [TestMethod]
        public void TestChangeCheckboxValue()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            Assert.IsNotNull(dataGrid);
            CheckBox checkBox = dataGrid[1, 3].GetNestedElement<CheckBox>();
            Assert.AreEqual(false, checkBox.IsChecked);
            checkBox.Check();
            Assert.AreEqual(true, checkBox.IsChecked);
        }

        [TestMethod]
        public void TestChangeText()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            Assert.AreEqual("World Of Warcraft", dataGrid[0, 0].TextValue);
            dataGrid[0, 0].TextValue = "WOW";
            Assert.AreEqual("WOW", dataGrid[0, 0].TextValue);
        }

        [TestMethod]
        [ExpectedException(typeof(OperationNotSupportedException))]
        public void TestTextReadNotSupported()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            Assert.AreEqual(String.Empty, dataGrid[1, 3].TextValue);
        }

        [TestMethod]
        [ExpectedException(typeof(OperationNotSupportedException))]
        public void TestTextChangeNotSupported()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            dataGrid[1, 3].TextValue = "Checkbox";
        }

        [TestMethod]
        [ExpectedException(typeof(OperationNotSupportedException))]
        public void TestTextChangeReadOnlyDataGrid()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGrid");
            dataGrid[0, 0].TextValue = "WOF";
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestTextChangeNullValue()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            dataGrid[0, 0].TextValue = null;
        }

        [TestMethod]
        public void TestSortRows()
        {
            DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("dataGridFancy");
            Assert.AreEqual("World Of Warcraft", dataGrid[0, 0].TextValue);
            Assert.AreEqual("Halo", dataGrid[1, 0].TextValue);
            Assert.AreEqual("Gears Of War", dataGrid[2, 0].TextValue);
            dataGrid.Header[1].Sort();
            Assert.AreEqual("Gears Of War", dataGrid[0, 0].TextValue);
            Assert.AreEqual("Halo", dataGrid[1, 0].TextValue);
            Assert.AreEqual("World Of Warcraft", dataGrid[2, 0].TextValue);
            dataGrid.Header[1].Sort();
            Assert.AreEqual("World Of Warcraft", dataGrid[0, 0].TextValue);
            Assert.AreEqual("Halo", dataGrid[1, 0].TextValue);
            Assert.AreEqual("Gears Of War", dataGrid[2, 0].TextValue);
        }
        // TODO: Deselektieren von Zellen (Remove from selection)
    }
}
